# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import json
class Pre_address:
    docId = 1
    # 输入参数为要分析的 html 文件名，返回值为对应的 BeautifulSoup 对象
    def create_doc_from_filename(self, filename):
        with open(filename, "r", encoding='utf-8') as f:
            html_content = f.read()
            doc = BeautifulSoup(html_content,'lxml')
            return doc
    def main(self):
        while True:
            try:
                doc = self.create_doc_from_filename("./jd_data/" + str(self.docId) + '.html') 
            except:
                break
            if doc.find(attrs = {'rel' : 'canonical'}):
                url = doc.find(attrs = {'rel' : 'canonical'}).attrs['href']
                if '//item.jd.com/' in url:
                    #标准商品界面，开始解析
                    dic = {}
                    #记录id
                    dic['id'] = self.docId
                    #记录url
                    dic['url'] = url
                    #商品标题
                    dic['title'] = doc.title.text.encode('gbk', 'ignore').decode('gbk')
                    #商品名
                    dic['good_name'] = doc.find(class_='item ellipsis').text.encode('gbk', 'ignore').decode('gbk')
                    #店铺名
                    for store_name in doc.find_all(attrs={'class':'name'}):
                        if store_name.a and store_name.a.attrs['target'] == "_blank":
                            dic['store_name'] = store_name.a.text.encode('gbk', 'ignore').decode('gbk')
                    
                    #商品评价,物流履约,售后服务 *
                    dic['score'] = {}
                    score_list = doc.find_all(attrs={'class':'score-part'})
                    if score_list:
                        for score in score_list:
                            #获取键
                            for n in score.contents:
                                if n != '\n' and n.attrs['class'][0] == 'score-desc':
                                    dic['score'][n.text.encode('gbk', 'ignore').decode('gbk')] = score.em.text.encode('gbk', 'ignore').decode('gbk')      
                                    
                    #商品品牌
                    brand = doc.find(attrs={'id':'parameter-brand'})
                    if brand:
                        dic['brand'] = brand.text.replace('\n', '').replace('品牌： ', '').encode('gbk', 'ignore').decode('gbk')
                    #商品参数
                    dic['parameter'] = []
                    parameter = doc.find(class_ = 'parameter2 p-parameter-list')
                    if parameter:
                        for para in parameter.contents:
                            if para != '\n':
                                dic['parameter'].append(para.text.encode('gbk', 'ignore').decode('gbk'))
                    #选择种类
                    dic['kinds'] = []
                    kinds = doc.find(attrs={'id':'choose-attrs'})
                    if kinds:
                        if len(kinds.contents) > 1 and len(kinds.contents[1].contents) > 3:
                            kinds = kinds.contents[1].contents[3].contents[1::2]
                            for kind in kinds:
                                dic['kinds'].append(kind.attrs['data-value'].encode('gbk', 'ignore').decode('gbk'))
                    
                    #商品详细信息 *
                    dic['detail'] = []
                    detail_infos = doc.find_all(class_ = "Ptable-item")
                    for detail in detail_infos:
                        detail_dic = {}
                        #主标题
                        detail_dic['head'] = detail.h3.text.encode('gbk', 'ignore').decode('gbk')
                        detail_dic['content'] = []
                        for dl in detail.dl.contents:
                            if dl != '\n':
                                dl_dic = {'dd': []}
                                for d in dl.contents:
                                    if d != '\n':
                                        if d.name == 'dt':
                                            dl_dic['dt'] = d.text.replace('\n', '').encode('gbk', 'ignore').decode('gbk')
                                        elif d.name == 'dd':
                                            dl_dic['dd'].append(d.text.replace('\n', '').encode('gbk', 'ignore').decode('gbk'))
                                detail_dic['content'].append(dl_dic)
                        dic['detail'].append(detail_dic)
                        
                    #print(dic)

                    with open("./json/" + str(self.docId) + ".json", 'w') as write_f:
                        json.dump(dic, write_f, indent=4, ensure_ascii=False)

            self.docId += 1

if __name__ == '__main__':
    pre = Pre_address()
    pre.main()